package com.ruoyi.mhrb.order.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.mhrb.custom.Payservice.CustomPayService;
import com.ruoyi.mhrb.custom.model.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mhrb.order.mapper.MhrbOrderMapper;
import com.ruoyi.mhrb.order.domain.MhrbOrder;
import com.ruoyi.mhrb.order.service.IMhrbOrderService;

/**
 * 订单列Service业务层处理
 *
 * @author jhrb
 * @date 2024-11-26
 */
@Service
public class MhrbOrderServiceImpl implements IMhrbOrderService {
    @Autowired
    private MhrbOrderMapper mhrbOrderMapper;

    @Autowired
    private CustomPayService payService;

    /**
     * 查询订单列
     *
     * @param orderId 订单列主键
     * @return 订单列
     */
    @Override
    public MhrbOrder selectMhrbOrderByOrderId(Long orderId) {
        return mhrbOrderMapper.selectMhrbOrderByOrderId(orderId);
    }

    /**
     * 查询订单列列表
     *
     * @param mhrbOrder 订单列
     * @return 订单列
     */
    @Override
    public List<MhrbOrder> selectMhrbOrderList(MhrbOrder mhrbOrder) {
        return mhrbOrderMapper.selectMhrbOrderList(mhrbOrder);
    }

    /**
     * 新增订单列
     *
     * @param mhrbOrder 订单列
     * @return 结果
     */
    @Override
    public int insertMhrbOrder(MhrbOrder mhrbOrder) {
        mhrbOrder.setCreateTime(DateUtils.getNowDate());
        mhrbOrder.setOrderNum(Seq.getOrderNum());
        mhrbOrder.setOrderStatus("0");
        PayResult payUrl = new PayResult();
        if (Objects.equals(1L, mhrbOrder.getPayWay())) {
            payUrl = payService.getPayUrl(mhrbOrder.getOrderName(), mhrbOrder.getOrderMoney().toString(), mhrbOrder.getOrderNum());
        } else {
            mhrbOrder.setOrderMoney(mhrbOrder.getFinalMoney().add(mhrbOrder.getEarnestMoney()));
            payUrl = payService.getPayUrl(mhrbOrder.getOrderName() + "(定金)", mhrbOrder.getEarnestMoney().toString(), mhrbOrder.getOrderNum());
        }
        if (payUrl.getCode() == 1) {
            mhrbOrder.setPayUrl(payUrl.getPayurl());
            return mhrbOrderMapper.insertMhrbOrder(mhrbOrder);
        } else {
            return 0;
        }
    }


    /**
     * 修改订单列
     *
     * @param mhrbOrder 订单列
     * @return 结果
     */
    @Override
    public int updateMhrbOrder(MhrbOrder mhrbOrder) {
        return mhrbOrderMapper.updateMhrbOrder(mhrbOrder);
    }

    /**
     * 批量删除订单列
     *
     * @param orderIds 需要删除的订单列主键
     * @return 结果
     */
    @Override
    public int deleteMhrbOrderByOrderIds(Long[] orderIds) {
        return mhrbOrderMapper.deleteMhrbOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单列信息
     *
     * @param orderId 订单列主键
     * @return 结果
     */
    @Override
    public int deleteMhrbOrderByOrderId(Long orderId) {
        return mhrbOrderMapper.deleteMhrbOrderByOrderId(orderId);
    }
}
